//
//  ViewController.m
//  获取图片名称
//
//  Created by chenleping on 2018/7/17.
//  Copyright © 2018年 IAPTest. All rights reserved.
//

#import "ViewController.h"

#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *picView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor yellowColor];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    return;
    UIImage* image = [UIImage imageNamed:@"wo.png"];
    NSData * imageData = UIImageJPEGRepresentation(image, 0.85);
    NSInteger length = imageData.length;
    ALAssetsLibrary *library  = [[ALAssetsLibrary alloc] init];
    
    [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error) {
        if (error) {
            NSLog(@"error:===%@", error);
        }else{
            PHFetchResult *fetchResult = [PHAsset fetchAssetsWithALAssetURLs:@[assetURL] options:nil];
            PHAsset *asset = fetchResult.firstObject;
            PHImageManager * imageManager = [PHImageManager defaultManager];
            [imageManager requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
                    NSLog(@"info:==%@", info);
                    NSURL *url = [info valueForKey:@"PHImageFileURLKey"];
                    NSString *str = [url absoluteString];   //url>string
                    NSArray *arr = [str componentsSeparatedByString:@"/"];
                    NSString *imgName = [arr lastObject];
                NSLog(@"imgName:===%@", imgName);
            }];
            
        }
    }];

}

- (IBAction)selectPic:(id)sender {
    UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.editing = YES;
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [self presentViewController:imagePicker animated:YES completion:nil];

}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    //赋值并且移除控制器
    self.picView.image = info[UIImagePickerControllerOriginalImage];
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    NSURL *imageUrl = [info valueForKey:UIImagePickerControllerReferenceURL];
    PHFetchResult *fetchResult = [PHAsset fetchAssetsWithALAssetURLs:@[imageUrl] options:nil];
    PHAsset *asset = fetchResult.firstObject;
    PHImageManager * imageManager = [PHImageManager defaultManager];
    [imageManager requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
        NSLog(@"info:==%@", info);
        NSURL *url = [info valueForKey:@"PHImageFileURLKey"];
        NSString *str = [url absoluteString];   //url>string
        NSArray *arr = [str componentsSeparatedByString:@"/"];
        NSString *imgName = [arr lastObject];
        NSLog(@"imgName:===%@", imgName);
    }];
    
}


@end
